from django.db import models
from django.contrib.auth.models import AbstractUser

from utils.common_basemodel import *


# 用户账号表
class User(AbstractUser):
    type = (
        (1, '管理员'),
        (2, '普通医生'),
        (3, '药管师'),
    )
    icon = models.ImageField(upload_to='icon', default='icon/default.png', verbose_name='头像')
    user_type = models.SmallIntegerField(choices=type, default=2, verbose_name='用户类型')
    doctor = models.OneToOneField(to='Doctor', on_delete=models.CASCADE, blank=True, null=True,
                                  verbose_name='医生')

    def user_state(self):
        if self.is_active:
            return '正常'
        return '未激活'

    class Meta:
        db_table = 'hospital_user'
        verbose_name = '后台用户表'
        verbose_name_plural = verbose_name

    # def __str__(self):
    #     return self.doctor.name

    def user_type_name(self):
        return self.get_user_type_display()


# 医生表
class Doctor(BaseModels,ModelRedMethod):
    assess = models.CharField(max_length=32, default='暂未评分', verbose_name='用户评价')
    department = models.ForeignKey(to='Department', default=1, null=True, on_delete=models.CASCADE)

    class Meta:
        db_table = 'hospital_doctor'
        verbose_name = '医生表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    def department_name(self):
        return self.department.name

    def job(self):
        return self.user.user_type


# 科室表
class Department(models.Model):
    type = (
        (1, '正畸美容'),
        (2, '综合医疗'),
        (3, '暂未划分'),
    )
    name = models.CharField(max_length=32, verbose_name='科室名')
    department_code = models.CharField(max_length=32, verbose_name='科室编码', null=True)
    add_name = models.CharField(max_length=32, verbose_name='创建人')
    add_time = models.DateTimeField(auto_now_add=True)
    depart_type = models.SmallIntegerField(choices=type, default=3, verbose_name='科室所属类型')

    class Meta:
        db_table = 'hospital_department'
        verbose_name = '科室表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    def depart_type_name(self):
        return self.get_depart_type_display()

    def doctor_type(self):
        doctor_opt = []
        for doctor_obj in self.doctor_set.all():
            doctor_opt.append({'id': doctor_obj.id,
                               'label': doctor_obj.name})
        return doctor_opt


# 药物管理表
class Medicine(models.Model):
    name = models.CharField(max_length=32, verbose_name='药物名')
    med_number = models.CharField(max_length=32, verbose_name='药物编号')
    category = models.CharField(max_length=32, verbose_name='药物类型')
    total = models.IntegerField(verbose_name='药物数量')
    is_ground = models.BooleanField(default=False, verbose_name='是否上架')
    price = models.CharField(max_length=32, verbose_name='药物价格')
    department = models.ManyToManyField(to='Department')
    ground_num = models.IntegerField(null=True, blank=True, verbose_name='药品上架的数量')

    class Meta:
        verbose_name_plural = '药物表'

    def __str__(self):
        return self.name
